"""
 encoding    : utf-8
 @File       : den_lu.py
 @Author     : jyy
 @Version    :
 @Description: 单个模块测试时临时使用的登录方法类
 @Date       : 2024/07/11
"""


from selenium import webdriver
from selenium.webdriver.common.by import By

class deng_lu():
    def da_kai_llq(self,driver,url):
        self.driver = driver                    # 获取传入的浏览器实例
        self.driver.get(url)                    # 用传入的浏览器实例打开传入的url网址
        self.driver.set_window_size(1200,900)   # 调整窗口大小

    def guan_bi_llq(self,driver):
        driver.quit()                           # 关闭浏览器


    def deng_lu(self,driver,user,pwd,yan_zheng_ma):
        self.driver = driver                    # 获取传入的浏览器实例
        self.driver.implicitly_wait(5)          # 静态等待最多5秒
        try :
            ele = self.driver.find_element(By.XPATH,
                                           "/html/body/div/div/div[1]/form/div[1]/div/div/span/div/input")  # 定位用户名输入框
            ele.click()                             # 点击用户名输入框
            ele.send_keys(user)                     # 输入用户名
        except:
            return '定位用户名输入框失败'

        try :
            ele = self.driver.find_element(By.XPATH,
                                           "/html/body/div/div/div[1]/form/div[2]/div/div/span/div/input")  # 定位密码输入框
            ele.click()                             # 点击密码名输入框
            ele.send_keys(pwd)                     # 输入密码
        except:
            return '定位密码输入框失败'

        try :
            ele = self.driver.find_element(By.XPATH,
                                           "/html/body/div/div/div[1]/form/div[3]/div/div/span/span/div/input")  # 定位验证码输入框
            ele.click()                             # 点击验证码输入框
            ele.send_keys(yan_zheng_ma)
        except:
            return '定位验证码输入框失败'

        try :
            ele = self.driver.find_element(By.XPATH,
                                           "/html/body/div/div/div[1]/form/div[5]/div/div/span/button")  # 定位登录按钮
            ele.click()                             # 点击登录按钮
        except:
            return '定位登录按钮失败'

        try:
            ele = self.driver.find_element(By.XPATH,'/html/body/div/section/section/header/div/div[1]')  # 定位右上角账号文本
            # print(ele.text)
            if ele.text == f'欢迎{user}' :           # 判断登录结果
                return '登录成功'
        except :
            return '登录失败'

# /html/body/div/div/div[1]/form/div[1]/div/div/span/div/input(用户名)
# /html/body/div/div/div[1]/form/div[2]/div/div/span/div/input(密码)
# /html/body/div/div/div[1]/form/div[3]/div/div/span/span/div/input(验证码)
# /html/body/div/div/div[1]/form/div[5]/div/div/span/button(登录按钮)